package graduate.cslg.com.universityshopping.mainPage;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.utils.SimpleTextWatcher;

public class UserInfoManagerActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText userAccount;
    private EditText userName;
    private EditText userPass;
    private EditText userTel;
    private EditText userDorId;
    private EditText userSex;
    private EditText userDepartment;
    private EditText userType;
    private EditText userGrade;
    private EditText userDorInfo;
    private Button submitButton;
    private ProgressDialog progressDialog;

    TextWatcher textWatcher = new SimpleTextWatcher() {
        @Override
        public void afterTextChanged(Editable editable) {
            updateInfo();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info_manager);
        initViews();
        updateInfo();
        submitButton.setOnClickListener(this);
    }

    private void initViews() {
        userAccount = findViewById(R.id.stu_account);
        userName = findViewById(R.id.stu_name);
        userPass = findViewById(R.id.stu_password);
        userTel = findViewById(R.id.stu_tel);
        userDorId = findViewById(R.id.stu_dor_id);
        userSex = findViewById(R.id.stu_sex);
        userDepartment = findViewById(R.id.stu_department);
        userType = findViewById(R.id.stu_type);
        userGrade = findViewById(R.id.stu_grade);
        userDorInfo = findViewById(R.id.stu_dor_info);
        submitButton = findViewById(R.id.submit_button);
        userAccount.addTextChangedListener(textWatcher);
    }

    private void updateInfo() {
        if (userAccount.length() == 9) {
            showProgressDialog();
            getUserInfo();
        }
    }

    private void getUserInfo() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                updateUserInfo();
                break;
        }
    }

    private void updateUserInfo() {

    }

    /**
     * 显示进度对话框
     */
    private void showProgressDialog(){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(this);
            progressDialog.setMessage("用户信息验证中，请稍等");
            progressDialog.setCanceledOnTouchOutside(false);
        }
        progressDialog.show();
    }
    /**
     * 关闭进度对话框
     */
    private void closeProgressDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
        }
    }
}
